package com.njtoyo.taxi.admin.rest.controller.business.geo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.njtoyo.taxi.admin.aop.PermissionScope;
import com.njtoyo.taxi.admin.rest.presenter.business.geo.AllListPresenter;
import com.njtoyo.taxi.admin.rest.presenter.business.geo.RegionListPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.canton.DriverOrderSetWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.canton.RegionCreateWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.canton.RegionQueryWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.canton.RegionUpdateWrapper;
import com.njtoyo.taxi.admin.service.business.geo.GeoDriverRegionService;
import com.taxi.entity.common.RestResult;
import com.njtoyo.taxi.entity.backend.AdminUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value = "司机运营区", tags = "司机运营区")
@RequestMapping("business/geo/driver_region")
public class DriverRegionController {

    @Autowired
    private GeoDriverRegionService geoDriverRegionService;

    @GetMapping("/all")
    @ApiOperation(value = "全部司机运营区", notes = "无接口权限校验")
    RestResult<List<AllListPresenter>> getAll(@ModelAttribute("admin") AdminUser adminUser) {
        return geoDriverRegionService.getAll(adminUser);
    }

    @PostMapping
    @ApiOperation(value = "添加司机运营区", notes = "权限：driver_region_create")
    @PermissionScope(names = {"driver_region_create"})
    RestResult create(@Valid @RequestBody RegionCreateWrapper wrapper) {
        return geoDriverRegionService.create(wrapper);
    }

    @GetMapping()
    @ApiOperation(value = "司机运营区列表", notes = "地区列表：权限：driver_region_list")
    @PermissionScope(names = {"driver_region_list"})
    RestResult<IPage<RegionListPresenter>> getList(RegionQueryWrapper wrapper) {
        return geoDriverRegionService.getList(wrapper);
    }

    @PatchMapping("/{id}")
    @ApiOperation(value = "修改司机运营区", notes = "权限：driver_region_update")
    @PermissionScope(names = {"driver_region_update"})
    RestResult<Boolean> update(@PathVariable Long id, @RequestBody RegionUpdateWrapper wrapper) {
        return geoDriverRegionService.update(id, wrapper);
    }

    @PostMapping("/updateState/{id}")
    RestResult<Boolean> updateState(@PathVariable Long id, @RequestBody RegionUpdateWrapper wrapper) {
        return geoDriverRegionService.updateState(id, wrapper);
    }

    @GetMapping("/getRegion/{areaId}")
    RestResult<Boolean> getRegionByAreaId(@PathVariable Long areaId) {
        return geoDriverRegionService.getRegionByAreaId(areaId);
    }


    /**
     * 接单配置
     *
     * @param
     * @return
     */
    @GetMapping("/getOrderSet/{driverRegionId}")
    RestResult getOrderSet(@PathVariable Long driverRegionId) {
        return geoDriverRegionService.getOrderSet(driverRegionId);
    }


    /**
     * 接单配置
     *
     * @param
     * @return
     */
    @PostMapping("/orderSet")
    RestResult orderSet(@RequestBody DriverOrderSetWrapper wrapper) {
        if (ObjectUtil.isNull(wrapper.getDriverRegionId())) {
            return RestResult.failed("司机运营区为空");
        }
        if (CollUtil.isEmpty(wrapper.getRules())) {
            RestResult.failed("配置项为空");
        }
        return geoDriverRegionService.orderSet(wrapper);
    }

}
